package com.qp.basics.estivate;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 * 表属性包装类。 现在属性并不全面。
 * 
 */
public class Table {
	private Map properties = new HashMap();
	private String schema;
	private Database database;
	private Column[] columns;
	private String tableName;
	private String tableType;
	/** 注释 */
	private String comment;
	private Map<String, String> checkConstraints = new TreeMap<String, String>(
			String.CASE_INSENSITIVE_ORDER);
	private Map<String, RelationShip> relationMap = new TreeMap<String, RelationShip>();
	private Map<String, Index> indexMap = new TreeMap<String, Index>();
	private final static Logger logger = Logger
			.getLogger(Table.class.getName());

	public Table(String tableName) {
		this.tableName = tableName;
	}

	public Table() {
	}

	public void setColumns(Column[] columns) {
		this.columns = columns;
	}

	public Column[] getColumns() {
		if (columns == null) {
			columns = database.getColumns(this);
		}
		return columns;
	}

	public Column[] getPks() {
		// if(pks == null){//如果这样保持其敏感性比较麻烦
		getColumns();
		Vector v = new Vector();
		for (int i = 0; i < columns.length; i++) {
			Column col = columns[i];
			if (col.isPk()) {
				v.add(col);
			}
		}
		Column[] pks = new Column[v.size()];
		v.toArray(pks);
		// }
		return pks;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTableType() {
		return tableType;
	}

	public void setTableType(String tableType) {
		this.tableType = tableType;
	}

	public Database getDatabase() {
		return database;
	}

	public void setDatabase(Database database) {
		this.database = database;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Map getProperties() {
		return properties;
	}

	public void putProperties(Map properties) {
		this.properties.putAll(properties);
	}

	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public Column getColumn(String colName) {
		Column[] columns = this.getColumns();
		for (int i = 0; i < columns.length; i++) {
			Column col = columns[i];
			if (col.getColumnName().equalsIgnoreCase(colName)) {
				return col;
			}
		}
		return null;
	}

	public Map<String, String> getCheckConstraints() {
		return checkConstraints;
	}

	public void setCheckConstraints(Map<String, String> checkConstraints) {
		this.checkConstraints = checkConstraints;
	}

	public int compareTo(Table other) {
		if (other == this) // fast way out
			return 0;

		int rc = getTableName().compareToIgnoreCase(other.getTableName());
		if (rc == 0) {
			// should only get here if we're dealing with cross-schema
			// references (rare)
			String ours = getSchema();
			String theirs = other.getSchema();
			if (ours != null && theirs != null)
				rc = ours.compareToIgnoreCase(theirs);
			else if (ours == null)
				rc = -1;
			else
				rc = 1;
		}

		return rc;
	}

	/**
	 * Returns <code>true</code> if this table is remote (in another schema),
	 * <code>false</code> otherwise
	 * 
	 * @return
	 */
	public boolean isRemote() {
		return false;
	}

	public void addCheckConstraint(String constraintName, String text) {
		checkConstraints.put(constraintName, text);
	}

	public void addIndex(String indexName, String nonUnique, Column column) {
		Index index = indexMap.get(indexName);
		if (index == null) {
			index = new Index();
			index.setName(indexName);
			if (nonUnique.equals("1")) {
				index.setIsUnique("否");
			} else {
				index.setIsUnique("是");
			}
			index.addColumn(column);
			index.setTablel(this);
			indexMap.put(indexName, index);
		} else {
			if (!index.checkColumn(column)) {
				index.addColumn(column);
			} else {
				System.out.println("已经存在列了，请检查------------");
			}
		}
	}

	public void addForeignKeys(String name, Table parentTable,
			Column parentColumn, Table childTable, Column childColumn,
			Column currentColum, int deleteRule, int updateRule,
			Pattern excludeIndirectColumns, Pattern excludeColumns) {
		RelationShip relationShip = relationMap.get(name);
		if (relationShip == null) {
			relationShip = new RelationShip(name, parentTable, parentColumn,
					childTable, childColumn, currentColum, deleteRule,
					updateRule);
			relationMap.put(name, relationShip);
		}

		// 先不处理child
		// if (parentColumn != null) {
		// foreignKey.setChildColumn(childColumn);
		// String parentSchema = pkTableSchema;
		// Column parentColumn = pkColumn;
		// if (parentTable != null) {
		// foreignKey.setParentColumn(parentColumn);
		// childColumn.addParent(parentColumn, foreignKey);
		// parentColumn.addChild(childColumn, foreignKey);
		// } else {
		// logger.warning("Couldn't add FK '" + foreignKey.getName() +
		// "' to table '" + this +
		// "' - Unknown Referenced Table '" + pkTableName + "'");
		// }
		// } else {
		// logger.warning("Couldn't add FK '" + foreignKey.getName() +
		// "' to table '" + this +
		// "' - Column '" + fkColName + "' doesn't exist");
		// }
	}

	public Map<String, RelationShip> getRelationMap() {
		return relationMap;
	}

	public void setRelationMap(Map<String, RelationShip> relationMap) {
		this.relationMap = relationMap;
	}

	public Map<String, Index> getIndexMap() {
		return indexMap;
	}

	public void setIndexMap(Map<String, Index> indexMap) {
		this.indexMap = indexMap;
	}

}
